package com.mall.controller;

import com.mall.bean.SysUser;
import com.mall.bean.SystemRole;
import com.mall.bean.SystemUser;
import com.mall.service.SystemPermissionService;
import com.mall.service.SystemUserRoleService;
import com.mall.service.SystemUserService;
import com.mall.serviceImpl.SystemPermissionServiceImpl;
import com.mall.serviceImpl.SystemUserServiceImpl;
import com.mall.util.SecurityUtil;
import com.mall.vo.Response;
import com.mall.vo.RoleVo;
import com.mall.vo.SysUserVo;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.*;

/**
 * @PageName com.mall.controller
 * @Description 系统用户管理控制层
 * @Author 何全裕
 * @Create 2017-09-06 22:26
 * @Version 1.0
 **/
@Controller
@RequestMapping("/user")
public class SystemUserController {
    @Autowired
    private SystemUserService systemUserService;

    @Autowired
    private SystemUserRoleService systemUserRoleService;

    @ResponseBody
    @RequestMapping("/getUserListForPage")
    public Map<String,Object> getUserListForPage() {
        Map<String,Object> map = new HashMap<String, Object>();
        List<SystemUser> userList = systemUserService.getSystemUserList();
        List<SysUserVo> userVoList = new ArrayList<SysUserVo>();
        for (SystemUser user : userList){
            SysUserVo userVo = new SysUserVo();
            try {
                BeanUtils.copyProperties(userVo, user);
            } catch (Exception e) {
                e.printStackTrace();
            }
            userVo.setLockedVo(user.getLocked()==0?"未锁定":"锁定");
            userVoList.add(userVo);
        }
        map.put("code",0);
        map.put("msg","");
        map.put("count",userVoList.size());
        map.put("data",userVoList);
        return map;
    }


    @RequestMapping("/getUserList")
    public ModelAndView getUserList() {
        ModelAndView modelAndView = new ModelAndView("/system_user/SystemUser");
        return modelAndView;
    }

    @RequestMapping("/toUserAdd")
    public ModelAndView toUserAdd() {
        ModelAndView modelAndView = new ModelAndView("/system_user/SystemUserAdd");
        return modelAndView;
    }

    @ResponseBody
    @RequestMapping("/insertUser")
    public Response insertUser(SystemUser user) {
        user.setId(UUID.randomUUID().toString());
        String salt = UUID.randomUUID().toString().substring(0,4);
        user.setSalt(salt);
        user.setLocked(0);
        user.setPassword(SecurityUtil.getEncryotString(user.getPassword(),salt));
        systemUserService.insertUser(user);
        return new Response("0","","");
    }

    @ResponseBody
    @RequestMapping("/deleteAllUser")
    public Response deleteAllUser(@RequestParam(value="ids[]") String[] ids) {
        systemUserService.deleteAllUser(ids);
        for(String id : ids){
            systemUserRoleService.deleteUserRoleByUserId(id); //删除用户角色关联表相关数据
        }
        return new Response("0","","");
    }

    @ResponseBody
    @RequestMapping("/deleteUser")
    public Response deleteUser( String id) {
        systemUserService.deleteUser(id);
        systemUserRoleService.deleteUserRoleByUserId(id); //删除用户角色关联表相关数据
        return new Response("0","","");
    }


    @RequestMapping("/getSystemUserById")
    public ModelAndView getSystemUserById(String id) {
        ModelAndView modelAndView = new ModelAndView("/system_user/SystemUserUpdate");
        SystemUser user = systemUserService.getSystemUserById(id);
        modelAndView.addObject("vo",user);
        return modelAndView;
    }

    @ResponseBody
    @RequestMapping("/updateSystemUser")
    public Response updateSystemUser( SystemUser user) {
        systemUserService.updateSystemUser(user);
        return new Response("0","","");
    }
}
